<?php
namespace addons\travel\model;

use think\Model;
use addons\travel\model\Service as ServiceModel;

/**
 * 配置模型
 */
class Goods extends Model
{

    // 表名,不含前缀
    protected $name = 'travel_goods';
    // 追加属性
    protected $append = [
    ];

    public function getImageAttr($value, $data)
    {
        if (!empty($value)) return cdnurl($value, true);

    }

    public function getImagesAttr($value, $data)
    {
        $imagesArray = [];
        if (!empty($value)) {
            $imagesArray = explode(',', $value);
            foreach ($imagesArray as &$v) {
                $v = cdnurl($v, true);
            }
            return $imagesArray;
        }
        return $imagesArray;
    }

    public function getContentAttr($value, $data)
    {
        $content = $data['content'];
        $content = str_replace("<img src=\"/uploads", "<img style=\"width: 100%;!important\" src=\"" . cdnurl("/uploads", true), $content);
        $content = str_replace("<video src=\"/uploads", "<video style=\"width: 100%;!important\" src=\"" . cdnurl("/uploads", true), $content);
        return $content;

    }

    // 商品详情
    public static function detail($params)
    {
        $id=$params['id'];
        $lang_slug=$params['lang_slug'];
        $record = (new self())->where('state','in',[1,2]);
        if (isset($id)) {
            $record = $record->where('id', $id);
        }
        $record = $record->find();
        if(!$record){
            return false;
        }
        //处理特殊日期价格
        if($record['date_price']){
            $record['date_price']=json_decode($record['date_price'],true);
        }else{
            $record['date_price']=[];
        }
        //处理标签
        $service_records=[];
        $service_ids=$record['service_ids'];
        unset($record['service_ids']);
        if($service_ids){
            $service_records=ServiceModel::where('id','in',$service_ids)->select();
            foreach ($service_records as &$service_record){
                $service_lang_record=doLangData($service_record['lang_data'],$lang_slug);
                //合并数组 - 在模型中配置属性，如 image 使用 cdnurl,是在最终的结果时处理的。所以这里直接覆盖 image 就行，不用提前在处理数据
                if(!empty($service_lang_record)){
                    foreach ($service_lang_record as $slk=>$slv){
                        $service_record[$slk]=$slv;
                    }
                }
                unset($service_record['lang_data']);
            }
        }
        $record['service']=$service_records;
        //处理协议-列表里面不处理协议-详情再处理-和前端商量好再调一次协议接口。详情里也就不用处理了
        $lang_record=doLangData($record['lang_data'],$lang_slug);
        //合并数组 - 在模型中配置属性，如 image 使用 cdnurl,是在最终的结果时处理的。所以这里直接覆盖 image 就行，不用提前在处理数据
        if(!empty($lang_record)){
            foreach ($lang_record as $lk=>$lv){
                $record[$lk]=$lv;
            }
        }
        unset($record['lang_data']);
        return $record;
    }

}
